package red.service.impl;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import red.mapper.Textmapper;
import red.model.ArticleModel;
import red.model.LikeModel;
import red.service.TextService2;
import red.util.Model;

import java.util.List;
import java.util.UUID;

@Service
public class TextServiceImpl2 implements TextService2 {

    @Autowired
    private Textmapper textmapper;
    private List list;
    private JSONObject object;

    @Override
    public JSONObject addText(Model model) {
        String user_name = (String) model.get("user_name");
        String headline =(String) model.get("headline");
        String column_name = (String) model.get("column_name");
        String content =(String) model.get("content");
        String page = (String) model.get("page");
        String like_number =(String) model.get("like_number");
        String audit =(String) model.get("audit");
        ArticleModel articleModel= new ArticleModel();
        articleModel.setArticle_id(UUID.randomUUID().toString());
        articleModel.setUser_name(user_name);
        articleModel.setHeadline(headline);
        articleModel.setColumn_name(column_name);
        articleModel.setContent(content);
        articleModel.setPage(page);
        articleModel.setLike_number(like_number);
        articleModel.setAudit(audit);
        articleModel.setColumn_id(UUID.randomUUID().toString());
        articleModel.setUser_id(UUID.randomUUID().toString());
        textmapper.addText(articleModel);
        JSONObject list= JSONObject.fromObject(articleModel);
        return list;
    }

    @Override
    public List findTextid(Model model) {
        String article_id = (String) model.get("article_id");
        list =  textmapper.findTextid(article_id);
        if (list != null) {
            return list;
        }else {
            return null;
        }
    }

    @Override
    public JSONObject updatelike_num(Model model) {
        String article_id = (String) model.get("article_id");
        String like_num = (String) model.get("like_number");
        textmapper.updatelike_num(article_id,like_num);
        list = textmapper.selectlike_num(article_id,like_num);
        object = new JSONObject();
        if (list.size() > 0) {
            try {
                object.put("msg","点赞成功!");
                object.put("data",true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }else{
            try {
                object.put("msg","失败");
                object.put("data",false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }
    }

    @Override
    public JSONObject updatepage(Model model) {
        String article_id = (String) model.get("article_id");
        String page = (String) model.get("page");
        textmapper.updatepage(article_id,page);
        list = textmapper.selectpage(article_id,page);
        object = new JSONObject();
        if (list.size() > 0) {
            try {
                object.put("msg","浏览成功!");
                object.put("data",true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }else{
            try {
                object.put("msg","失败");
                object.put("data",false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }
    }

    @Override
    public JSONObject updateaudit(Model model) {
        String article_id = (String) model.get("article_id");
        String audit = (String) model.get("audit");
        textmapper.updateaudit(article_id,audit);
        list = textmapper.selectaudit(article_id,audit);
        object = new JSONObject();
        if (list.size() > 0) {
            try {
                object.put("msg","审核成功!");
                object.put("data",true);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }else{
            try {
                object.put("msg","审核失败");
                object.put("data",false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return object;
        }
    }

    @Override
    public LikeModel addlike(Model model) {
        String article_id = (String) model.get("article_id");
        String user_name = (String) model.get("user_name");
        String like_easy = (String) model.get("like_easy");
        LikeModel like= new LikeModel();
        like.setArticle_id(article_id);
        like.setUser_name(user_name);
        like.setLike_easy(like_easy);
        textmapper.addlike(like);
        return like;
    }

    @Override
    public List findlike(Model model) {
        String article_id = (String) model.get("article_id");
        String user_name = (String) model.get("user_name");
        List list = textmapper.findlike(article_id,user_name);
        return list;
    }
}
